import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { CommonService } from '../../../services-common/common.service';
import { GoodsRecommendPartsService } from '../goods-recommend-parts/goods-recommend-parts.service';

@Component({
  selector: 'app-goods-recommend-parts-c',
  templateUrl: './goods-recommend-parts-c.component.html',
  styleUrls: ['./goods-recommend-parts-c.component.scss']
})
export class GoodsRecommendPartsCComponent implements OnInit {

  partsTableData: Array<any> = [];
  partsLoading = false;

  /**
   * 选择更多商品Modal
   * ----------------------------------------------------------------
   */
  isGoodsVisible = false;
  modalTableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 10,
    total: 0
  };
  setOfCheckedId = new Set<number>();
  // 商品查询params
  modalSearchParams: any = {
    name: '',
    typeId: '',
    brandIds: ''
  };

  classifyOptions: Array<any> = [];
  brandOptions: Array<any> = [];

  submitLoading = false;
  deleteIdList: Array<any> = [];

  constructor(
    private msg: NzMessageService,
    private commonService: CommonService,
    private services: GoodsRecommendPartsService
  ) { }

  ngOnInit(): void {
    this.loadClassifyOptions()
    this.loadBrandOptions()

    this.loadTable()
  }

  // modal 分类options
  async loadClassifyOptions() {
    const {code, data, message}: any = await this.services.getPartsClassifyData();
    if (code === 0) {
      this.classifyOptions = data || [];
    }
  }

  // modal 品牌options
  async loadBrandOptions() {
    const {code, data, message}: any = await this.services.getPartsBrandData();
    if (code === 0) {
      this.brandOptions = data || [];
    }
  }

  async loadTable() {
    let params = {
      showType: 2, // showType  1.首页 2.配件
      userType: 1, // 1：C端 2：B端
      type: 1
    }
    const {code, data, message}: any = await this.services.getPartsRecommendGoodsData(params);
    if (code === 0) {
      this.partsTableData = data || [];
      this.deleteIdList = [];
    }
  }

  // Delete Item
  deletePartsItem(item: any) {
    this.deleteIdList.push(item.id);
    this.partsTableData = this.partsTableData.filter((d) => d.code !== item.code);
  }

  /**
   * ----------------------------------------------------------------
   * Select More Goods
   * ----------------------------------------------------------------
   */
  showMoreGoodsModal() {
    this.setOfCheckedId.clear();
    this.setOfCheckedId = new Set(this.partsTableData.map(m => m.code));

    this.loadModalGoodsData();
    this.isGoodsVisible = true;
  }

  // 查询商品list
  async loadModalGoodsData() {
    let params = {
      ...this.modalSearchParams,
      // showType: 2, // showType  1.首页 2.配件
      // userType: 1, // 1：C端 2：B端
      // type: 1,
      current: this.modalTableParams.page,
      size: this.modalTableParams.pageSize
    }
    const {code, data, message}: any = await this.services.getGoodsListData(params);

    if (code === 0) {
      const {records, total} = data || {};
      this.modalTableParams.tableData = records || [];
      this.modalTableParams.total = total || 0;
    }
  }

  // modal 查询
  queryModalGoodsData() {
    this.modalTableParams.page = 1;
    this.loadModalGoodsData();
  }

  // modal 重置
  resetModalGoodsData() {
    for (let key in this.modalSearchParams) {
      this.modalSearchParams[key] = ''
    }
    
    this.modalTableParams.page = 1;
    this.loadModalGoodsData();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.modalTableParams.page = index;
    this.loadModalGoodsData();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.modalTableParams.page = 1;
    this.modalTableParams.pageSize = sizeNum;
    this.loadModalGoodsData();
  }

  selectedGoods(item: any) {
    let obj = {
      goodsId: item.id,
      code: item.code,
      name: item.name,
      shortName: item.shortName,
      recommendStatus: [0, 2].includes(item.status) ? 0 : 1,
      priceB: item.shopPriceB,
      priceC: item.shopPriceC,
      goodsType: item.typeName,
      stockNum: item.stockNum,
      icon: item.icon,
      userType: item.userType,
      labelColor: "#ffffff",
      sort: 0
    }
    this.partsTableData = this.partsTableData.concat([obj]);
    // 添加code
    this.setOfCheckedId.add(item.code);
    // this.isGoodsVisible = false;
  }

  /**
   * 保存
   * ----------------------------------------------------------------
   */
  async submitData() {
    if (!this.verifData()) {
      return
    }

    let params = {
      deleteIdList: this.deleteIdList,
      recommendGoodsList: this.partsTableData,
      showType: 2, // 1：首页 2：配件
      type: 1,
      userType: 1
    }
    this.submitLoading = true;
    const {code, message}: any = await this.services.saveRecommendData(params)
    this.submitLoading = false;

    if (code === 0) {
      this.msg.success('保存成功');
      this.loadTable();
    }
  }

  verifData() {
    let satisfyArrs = this.partsTableData.filter(el => [0, 1].includes(el.userType));
    if (!this.partsTableData.length) {
      this.msg.warning('请先选择商品');
      return false;
    } else if (satisfyArrs.length < 6) {
      this.msg.warning('通用或者C端用户类型合计最少选择6条');
      return false;
    } else {
      return true;
    }
  }
}
